package com.xlh.dto.courseTest;

import com.google.common.collect.Lists;
import com.xlh.enums.LocalResourcesEnum;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.exception.common.GlobalException;
import com.xlh.pojo.courseTest.CourseQuestionRepository;
import com.xlh.pojo.courseTest.CourseQuestionRepositoryDetails;
import com.xlh.pojo.courseTest.CourseTestPaperAnswer;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/5/14
 */
@Data
public class ReadingDTO {
    // 题目id
    private Long id;
    // 题号
    private Integer orderNumber;
    // 题型
    private Integer type;
    // 题干
    private String content;
    // 正确答案
    private Object rightAnswer;
    // 用户答案
    private Object userAnswer;
    // 题目得分
    private Integer score;
    // 客观题是否正确
    private Boolean right;
    // 选择题选项
    private List<String> choiceList;
    // 是否多选题
    private Boolean isMultipleChoice;
    // 实验报告下载地址
    private String reportAddress;
    // 实验报告名称
    private String reportName;
    // 实验报告内容
    private String reportContents;
    // 实验报告查重警告列表
    private List<DuplicateResultDTO> resultList;
    //答案解析
    private String jxContent;
    private String jxContentHtml;
    private String userAnswerContentHtml;
    public ReadingDTO convert(
            Map<Long, AnswerSheetDTO> questionIdUserAnswerMap, Map<Long, List<CourseTestPaperAnswer>> questionIdAnswerListMap,
            Map<Long, List<DuplicateResultDTO>> questionDuplicateMap, Map<Long, UserPaperExperimentReportDTO> idExperimentMap
    ) {
        AnswerSheetDTO userAnswerDTO = questionIdUserAnswerMap.get(this.getId());

        // 选择题设置选项列表，正确答案选项，是否为多选题
        // 判断题设置答案是真（假）命题
        // 其余题设置参考答案
        List<CourseTestPaperAnswer> answers = questionIdAnswerListMap.get(this.getId());
        if (CollectionUtils.isEmpty(answers)) {
            throw new GlobalException("第" + this.getOrderNumber() + "题,找不到题目答案");
        }
        QuestionTypeEnum typeEnum = QuestionTypeEnum.forCode(this.getType());
        if (typeEnum != null) {
            switch (typeEnum) {
                case CHOICE:
                    List<Integer> rightAnswerList = Lists.newArrayList();
                    this.setChoiceList(answers.stream().map(CourseTestPaperAnswer::getContent).collect(Collectors.toList()));
                    for (CourseTestPaperAnswer choice : answers) {
                        if (choice.getIsCorrect()) rightAnswerList.add(choice.getOrderNumber());
                    }
                    this.setIsMultipleChoice(rightAnswerList.size() > 1);
                    this.setRightAnswer(rightAnswerList.toArray());
                    // 设置学生是否回答正确、学生答案
                    this.setRight(userAnswerDTO.getRight());
                    this.setUserAnswer(userAnswerDTO.getAnswer());
//                    this.setJxContent(answers);
                    break;

                case JUDGEMENT:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getIsCorrect());
                    // 设置学生是否回答正确、学生答案
                    this.setRight(userAnswerDTO.getRight());
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    break;

                case BLANK:
                case SAQ:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getContent());
                    // 设置学生答案
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    break;

                case EXPERIMENT:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getContent());

                    UserPaperExperimentReportDTO report = idExperimentMap.get(this.getId());
                    if (report != null) {
                        // 设置学生提交的word实验报告,在线实验报告
                        this.setReportAddress(LocalResourcesEnum.REPORT.getMappingUrl(report.getReportFile()));
                        this.setReportName(report.getReportFileName());
                        this.setReportContents(report.getReportHtml());
                    }
                    this.setResultList(questionDuplicateMap.get(this.getId()));
                    break;

                default:
                    break;
            }
        }
        // 设置学生是否回答正确、得分、学生答案
        this.setScore(userAnswerDTO.getScore());
        return this;
    }

    public ReadingDTO convert(Map<Long, AnswerSheetDTO> questionIdUserAnswerMap, Map<Long, List<CourseQuestionRepositoryDetails>> questionIdAnswerListMap, CourseQuestionRepository question) {
        AnswerSheetDTO userAnswerDTO = questionIdUserAnswerMap.get(this.getId());
        this.setType(question.getQuestionType());

        // 选择题设置选项列表，正确答案选项，是否为多选题
        // 判断题设置答案是真（假）命题
        // 其余题设置参考答案
        QuestionTypeEnum typeEnum = QuestionTypeEnum.forCode(this.getType());
        List<CourseQuestionRepositoryDetails> answers = questionIdAnswerListMap.get(this.getId());
        if(!typeEnum.name().equals("ENCLOSURE")){
            if (CollectionUtils.isEmpty(answers)) {
                throw new GlobalException("第" + this.getOrderNumber() + "题,找不到题目答案");
            }
        }
        if (typeEnum != null) {
            switch (typeEnum) {
                case CHOICE:
                    List<Integer> rightAnswerList = Lists.newArrayList();
                    this.setChoiceList(answers.stream().map(CourseQuestionRepositoryDetails::getContent).collect(Collectors.toList()));
                    for (CourseQuestionRepositoryDetails choice : answers) {
                        if (choice.getIsCorrect()) rightAnswerList.add(choice.getOrderNumber());
                    }
                    this.setIsMultipleChoice(rightAnswerList.size() > 1);
                    this.setRightAnswer(rightAnswerList.toArray());
                    // 设置学生是否回答正确、学生答案
                    this.setRight(userAnswerDTO.getRight());
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    break;

                case JUDGEMENT:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getIsCorrect());
                    // 设置学生是否回答正确、学生答案
                    this.setRight(userAnswerDTO.getRight());
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    break;

                case BLANK:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getMdContent());
                    // 设置学生答案
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    if (questionIdAnswerListMap.get(this.getId()).get(0).getMdContent().equals(userAnswerDTO.getAnswer())) {
                        if (userAnswerDTO.getRight()==false){
                            this.setRight(false);
                        }else {
                            this.setRight(true);
                        }
                    } else {
                        this.setRight(userAnswerDTO.getRight());
                    }
                    break;
                case SAQ:
                    this.setRightAnswer(questionIdAnswerListMap.get(this.getId()).get(0).getContent());
                    // 设置学生答案
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    this.setUserAnswerContentHtml(userAnswerDTO.getHtmlContent());
                    break;
                case ENCLOSURE:
                    // 设置学生答案
                    this.setUserAnswer(userAnswerDTO.getAnswer());
                    break;
                default:
                    break;
            }
        }
        // 设置学生是否回答正确、得分、学生答案
        this.setScore(userAnswerDTO.getScore());
        return this;
    }
}
